package com.sjsu.cmpe239.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sjsu.cmpe239.exceptions.DuplicateUserIdException;
import com.sjsu.cmpe239.services.UserService;
import com.sjsu.cmpe239.validations.UIMessages;

/**
 * Servlet implementation class register
 */
@WebServlet("/register")
public class register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		HttpSession session = request.getSession(true);
		RequestDispatcher dispatcher;

		UserService obj = new UserService();
		
		session.setAttribute("registerex", null);
		
		String username = request.getParameter("email");
		String password = request.getParameter("password");
		
		String fname = request.getParameter("fname");
		String lname = request.getParameter("lname");

		System.out.println("Received Request is: " + username +" , " + password +" , " + fname +" , " + lname);
		int result = 0;
	try
	{
		result = obj.registerUser(fname, lname, username, password);
		if(result == 1)
			{
			System.out.println("Registration successful!");
			
			//set the username and password in session
			session.setAttribute("username", username);
			session.setAttribute("passowrd", password);
			session.setAttribute("userType", 1);
			dispatcher = getServletContext().getRequestDispatcher("/select.jsp");
			dispatcher.forward(request, response);
			}
		else
			{
			System.out.println("could not connect to the database!");
			request.setAttribute("registerex", UIMessages.SERVICE_UNAVAILABLE);
			dispatcher = getServletContext().getRequestDispatcher("/register.jsp");
			dispatcher.forward(request, response);
			}
	}catch(DuplicateUserIdException e)
	{
		e.printStackTrace();
		request.setAttribute("registerex", UIMessages.DUPLICATE_EMAIL);
		dispatcher = getServletContext().getRequestDispatcher("/register.jsp");
		dispatcher.forward(request, response);
	}catch(Exception e)
	{
			e.printStackTrace();
			request.setAttribute("registerex", UIMessages.SERVICE_UNAVAILABLE);
			dispatcher = getServletContext().getRequestDispatcher("/register.jsp");
			dispatcher.forward(request, response);
	}
	}
}